﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.Statistics.IView;
using MINERP.MODEL.FoundationModel;
using MINERP.Statistics.Presenter;
using MINERP.BLL.Statistics;
using DevExpress.XtraGrid.Views.Grid;
using MINERP.COM;
using MINERP.ComControls;
using System.Reflection;
using MINERP.ComControls.Extension;

namespace MINERP.Statistics.Forms
{
    public partial class FrmBillEdit : FrmParent, IFrmBillEdit
    {
        public FrmBillEdit()
        {
            InitializeComponent();
            InitiAuthority();
            gridViewBill.ShowRowIndicator(50);
            gridViewDetaildetail.ShowRowIndicator(50);

        }
        public FrmBillEdit(int InBillID)
        {
            InitializeComponent();
            EditBillID = InBillID;
        }

        private int EditBillID = 0;
        private static FrmBillEdit instance = null;
        public static FrmBillEdit Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmBillEdit();
                }
                if (instance.IsDisposed)
                {
                    instance = new FrmBillEdit();
                }
                return instance;
            }
        }
        PBillEdit _Presenter;
        BillEditBLL _bll;
        private string userName = FrmMain.loginName;
        private void FrmBillEdit_Load(object sender, EventArgs e)
        {
            _Presenter = new PBillEdit(this);
            _bll = new BillEditBLL();
            _Presenter.Init();
            simpleButtonEdit.Enabled = false;
            if(EditBillID!=0)
            {
                textEditbianhao.Text = EditBillID.ToString();
                simpleButtonOK_Click(null,null);
            }
        }
        /// <summary>
        /// 用户不可用控件的限制
        /// </summary>
        public void InitiAuthority()
        {
            foreach (Control control in this.Controls)
            {
                foreach (string controlName in ControlsName)
                {
                    if (control.Name == controlName)
                    {
                        control.Visible = false;
                    }
                }
            }

            Type t = this.GetType();

            foreach (var item in t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField))
            {

                if (ControlsName.Contains(item.Name))
                {
                    object obj = item.GetValue(this);

                    if (obj.GetType() == typeof(ToolStripButton))
                    {
                        ToolStripButton it = obj as ToolStripButton;
                        it.Visible = false;
                    }

                    //else if (obj.GetType() == typeof(BarButtonItem))
                    //{
                    //    BarButtonItem it = (BarButtonItem)obj;
                    //    it.Visibility = BarItemVisibility.Never;
                    //}
                    else if (obj.GetType() == typeof(DevExpress.XtraNavBar.NavBarItem))
                    {
                        ((DevExpress.XtraNavBar.NavBarItem)obj).Visible = false;
                    }
                    //if (obj.GetType()==typeof(Control))
                    //{
                    else
                    {
                        try
                        {
                            ((Control)obj).Visible = false;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("无法对该控件进行限制" + ex.Message);
                        }

                    }
                    //}
                }
            }
        }

        #region IFrmBillEdit 成员


        public void DispalyShopBill(List<BillView> list)
        {
            gridControlBill.DataSource = list;
            gridControlBill.RefreshDataSource();
            gridControlDetaildetail.DataSource = null;
            gridControlDetaildetail.RefreshDataSource();
        }
        public void ShopBanDing(List<MODEL.FoundationModel.Shop_Info> shopList)
        {
            this.cmbShopList.DisplayMember = "ShopName";
            this.cmbShopList.ValueMember = "ShopId";
            cmbShopList.DataSource = shopList;
        }

        public int ShopId
        {
            get;
            set;
        }

        public DateTime StartTime
        {
            get;
            set;
        }
        public DateTime EndTime
        {
            get;
            set;
        }
        #endregion

        private void cmbShopList_DropDownClosed(object sender, EventArgs e)
        {
            if (cmbShopList.SelectedItem != null)
            {
                this.ShopId = Convert.ToInt32(((Shop_Info)cmbShopList.SelectedItem).ShopId);
            }
            else
            {
                this.ShopId = 0;
            }
        }

        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            this.simpleButtonOK.Enabled = false;
            try
            {
                if (textEditbianhao.Text.Trim() != "" && textEditbianhao.Text.Trim() != "0")
                {
                    _Presenter.DisPlayBill(Convert.ToInt32(textEditbianhao.Text));
                }
                else
                {
                    if (this.StartTime == null)
                    {
                        StartTime = DateTime.Today;
                    }
                    if (this.EndTime == null)
                    {
                        EndTime = DateTime.Today;
                    }
                    _Presenter.DisPlayBill();
                }
            }
            finally
            {
                this.simpleButtonOK.Enabled = true;
            }


        }

        private void dateEditStartTime_EditValueChanged(object sender, EventArgs e)
        {
           // this.StartTime = Convert.ToDateTime(dateEditStartTime.Text);
            this.StartTime = dateEditStartTime.Value;
        }

        private void dateEditEndTime_EditValueChanged(object sender, EventArgs e)
        {
           // this.EndTime = Convert.ToDateTime(dateEditEndTime.Text);
            this.EndTime = dateEditEndTime.Value;
        }

        private void gridViewBill_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                BillView view = (BillView)(gridViewBill.GetRow(e.RowHandle));

                e.ChildList = _Presenter.GetDetails(Convert.ToInt32(view.BillId));
                //isDetail = true;// 添加产品
            }

        }

        private void gridViewBill_MasterRowGetRelationCount(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationCountEventArgs e)
        {
            e.RelationCount = 1;
        }

        private void gridViewBill_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
        {
            e.RelationName = "账单产品";
        }

        private void ViewProduct_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle > 0)
            {
                //int intDetailId = Convert.ToInt32(((BillDetailView)(ViewProduct.GetRow(e.FocusedRowHandle))).BillDetailId);

                GridView view = sender as GridView;
                Currbilldetail = view.GetFocusedRow() as BillDetailView;



                isClickProduct = true;
                lists = _Presenter.GetDetailDetail(Currbilldetail.BillDetailId);
                gridControlDetaildetail.DataSource = lists;
                if (lists.Count > 0)
                {
                    simpleButtonEdit.Enabled = true;
                    simpleButtonSave.Enabled = true;
                    simpleButtonDel.Enabled = true;
                }
            }
        }
        BillDetailView Currbilldetail = new BillDetailView();
        List<Bill_Detail_Detail_Info> lists = new List<Bill_Detail_Detail_Info>();
        private void gridViewBill_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
        {
            e.IsEmpty = false;
        }

        private void gridViewDetaildetail_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                isDetail = false;///添加明细
                isClickProduct = false;
                isClickBill = false;
                isClickDetail = true;

                Bill_Detail_Detail_Info billdetail = view.GetFocusedRow() as Bill_Detail_Detail_Info;
                if (billdetail == null)
                {
                    return;
                }
                this.Type = billdetail.Type;
                textEditMingxi.Text = billdetail.DetailName;
                textEditAreas.Text = billdetail.Area.ToString("0.0000");
                textEditNum.Text = billdetail.Num.ToString();
                textEditunitprice.Text = billdetail.UnitPrice.ToString();
                //textEdit_sqc.Text = billdetail.Spec;
                //string spec = billdetail.Spec.Split('(')[0];
                textEdit_sqc.Text = billdetail.Spec;
                //textEditWeigth.Text = spec.Split('×')[0];
                //textEditHeigth.Text = spec.Split(' ')[0].Split('×')[1];
                textEditWeigth.Text = "0";
                textEditHeigth.Text = "0";
                textEditCount.Text = billdetail.Count.ToString("0.0000");
                CurrbilldetaildetailId = billdetail.Bill_Detail_DetailId;
                CurrInfo = billdetail;
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取数据失败，与服务器失去连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }
        bool isNew = false;
        bool islEdit = false;
        //  bool isDel = false;
        bool isDetail = false;
        bool CheckBillStatus(BillView inBill)
        {
            bool rtnBool = false;
            switch (inBill.status)
            {
                case (int)ShopBillStatus.CheckedOut:
                    rtnBool = true;
                    break;
                case (int)ShopBillStatus.UnCheckOut:
                    rtnBool = true;
                    break;
                case (int)ShopBillStatus.Delete:
                    rtnBool = false;
                    break;
                case (int)ShopBillStatus.Diverted:
                    rtnBool = false;
                    break;
                default:

                    rtnBool = false;
                    break;


            }
            return rtnBool;
        }

        private void simpleButtonNew_Click(object sender, EventArgs e)
        {
            
            //gridViewDetaildetail.OptionsView.NewItemRowPosition = NewItemRowPosition.Top; 
            //gridViewDetaildetail.col
            isNew = true;
            islEdit = false;
            // isDel = false;

            if (isNew && isClickBill)
            {



                simpleButtonEdit.Enabled = false;
                simpleButtonSave.Enabled = true;
                simpleButtonDel.Enabled = false;
                simpleButtonNew.Enabled = false;

                splitContainerNew.Panel1Collapsed = false;
                splitContainerDetailOrDDerail.Panel1Collapsed = true;
                splitContainerDetailOrDDerail.Panel2Collapsed = false;
                textEditOrderProductCode.Text = "";
                textEditProductName.Text = "";
                textEditCtmName.Text = "";
                textEditRemark.Text = "";

            }
            else if (isNew && isClickProduct)
            {


                simpleButtonEdit.Enabled = false;
                simpleButtonSave.Enabled = true;
                simpleButtonDel.Enabled = false;
                simpleButtonNew.Enabled = false;

                splitContainerNew.Panel1Collapsed = false;
                splitContainerDetailOrDDerail.Panel1Collapsed = false;
                splitContainerDetailOrDDerail.Panel2Collapsed = true;
                ;
                textEditMingxi.Enabled = true;

                textEditMingxi.Text = "";
                textEditAreas.Text = "";
                textEditNum.Text = "";
                textEditunitprice.Text = "";
                //textEdit_sqc.Text = "";
                textEditWeigth.Text = "";
                textEditHeigth.Text = "";
                textEditCount.Text = "";
                checkBoxIsShumashuchu.Checked = false;
            }

        }

        private void gridViewDetaildetail_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView view = sender as GridView;
            Bill_Detail_Detail_Info billdetail = view.GetFocusedRow() as Bill_Detail_Detail_Info;
            if (billdetail == null)
            {
                return;
            }

            textEditMingxi.Text = billdetail.DetailName;
            textEditAreas.Text = billdetail.Area.ToString("0.0000");
            textEditNum.Text = billdetail.Num.ToString();
            textEditunitprice.Text = billdetail.UnitPrice.ToString();
            textEdit_sqc.Text = billdetail.Spec;
            textEditCount.Text = billdetail.Count.ToString("0.0000");
            CurrbilldetaildetailId = billdetail.Bill_Detail_DetailId;
            CurrInfo = billdetail;
            if (billdetail.Type == 1)
            {
                textEditMingxi.Enabled = false;
                checkBoxIsShumashuchu.Checked = true;
            }



        }
        int CurrbilldetaildetailId = 0;
        Bill_Detail_Detail_Info CurrInfo = new Bill_Detail_Detail_Info();
        int Type = 0;//统计类型



        /// <summary>
        /// 判断添加信息的类型
        /// isClickProduct+isClickNew  增加明细
        /// isClickDetail+isClickEdit 修改明细
        /// isClickBill+isNew    增加产品
        /// isClickProduct+ isEdit 修改产品
        /// </summary>
        bool isClickProduct = false;
        bool isClickDetail = false;
        bool isClickBill = false;
        //bool isClickNew = false;
        //bool isClickEdit = false;


        bool isCheck = false;
        private void simpleButtonSave_Click(object sender, EventArgs e)
        {
            if (CurrBill != null && !string.IsNullOrEmpty(CurrBill.ShopName))
            {
                double changeValue = 0;
                if (isNew)
                {
                    isNew = false;
                    simpleButtonEdit.Enabled = true;
                    simpleButtonSave.Enabled = false;
                    simpleButtonDel.Enabled = true;
                    simpleButtonNew.Enabled = true;
                    splitContainerNew.Panel1Collapsed = true;

                    if (isClickBill)//添加产品部分
                    {
                        if (isCheckProduct)
                        {
                            Bill_Detail_Info info = new Bill_Detail_Info();
                            info.CtmName = textEditCtmName.Text.Trim(); ;
                            info.OrderInfoCode = textEditOrderProductCode.Text.Trim(); ;
                            info.OrderProductName = textEditProductName.Text.Trim();
                            info.Remark = textEditRemark.Text.Trim();
                            info.ListDetail_Detail = null;
                            _bll.InsertDetail(info, CurrBill.BillId);

                            MessageBox.Show("添加产品成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        else
                        {

                            if (_Presenter.GetProduct(this.textEditOrderProductCode.Text.Trim()) != null)
                            {
                                Bill_Detail_Info info = new Bill_Detail_Info();
                                info.CtmName = textEditCtmName.Text.Trim(); ;
                                info.OrderInfoCode = textEditOrderProductCode.Text.Trim(); ;
                                info.OrderProductName = textEditProductName.Text.Trim();
                                info.Remark = textEditRemark.Text.Trim();
                                info.ListDetail_Detail = null;
                                _bll.InsertDetail(info, CurrBill.BillId);

                                MessageBox.Show("添加产品成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }
                            else
                            {
                                if (IsCreateBill)
                                {
                                    Bill_Detail_Info info = new Bill_Detail_Info();
                                    info.CtmName = textEditCtmName.Text.Trim(); ;
                                    info.OrderInfoCode = textEditOrderProductCode.Text.Trim(); ;
                                    info.OrderProductName = textEditProductName.Text.Trim();
                                    info.Remark = textEditRemark.Text.Trim();
                                    info.ListDetail_Detail = null;
                                    _bll.InsertDetail(info, CurrBill.BillId);

                                    MessageBox.Show("添加产品成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    return;
                                }
                                else
                                {
                                    MessageBox.Show("产品不存在请检查产品code", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    return;
                                }
                            }
                        }
                        //splitContainerDetailOrDDerail.Panel1Collapsed = true;
                        //splitContainerDetailOrDDerail.Panel2Collapsed = false;


                        //        textEditOrderProductCode.Text = "";
                        //textEditProductName.Text = "";
                        //textEditCtmName.Text = "";
                        //textEditRemark.Text = ""

                    }
                    else if (isClickProduct)//添加明细部分
                    {
                        //splitContainerDetailOrDDerail.Panel1Collapsed = false;
                        //splitContainerDetailOrDDerail.Panel2Collapsed = true;

                        if (textEditMingxi.Text.Trim() == "")
                        {
                            MessageBox.Show("请添加收费明细", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;

                        }
                        Bill_Detail_Detail_Info info = new Bill_Detail_Detail_Info();
                        info.Area = Convert.ToDouble(textEditAreas.Text.Trim() == "" ? "0" : textEditAreas.Text.Trim());
                        if (Type == 1)
                        {
                            info.Type = 1;
                            info.Count = Convert.ToDouble(textEditAreas.Text.Trim() == "" ? "0" : textEditAreas.Text.Trim()) * Convert.ToDouble(textEditunitprice.Text.Trim() == "" ? "0" : textEditunitprice.Text.Trim());
                        }
                        else if (Type == 0)
                        {
                            info.Type = 0;
                            info.Count = Convert.ToDouble(textEditNum.Text.Trim() == "" ? "0" : textEditNum.Text.Trim()) * Convert.ToDouble(textEditunitprice.Text.Trim() == "" ? "0" : textEditunitprice.Text.Trim());

                        }

                        //info.Count = Convert.ToDouble(textEditNum.Text.Trim() == "" ? "0" : textEditNum.Text.Trim()) * Convert.ToDouble(textEditunitprice.Text.Trim() == "" ? "0" : textEditunitprice.Text.Trim());
                        info.DetailName = textEditMingxi.Text;
                        info.Spec = textEdit_sqc.Text;
                        Double weigth = Convert.ToDouble(textEditWeigth.Text);
                        Double heigth = Convert.ToDouble(textEditHeigth.Text);
                        info.Spec = textEdit_sqc.Text;// +weigth.ToString("0.00") + "×" + heigth.ToString("0.00") + "(" + (Convert.ToDouble(weigth) * 2.54).ToString("0.00") + "×" + (Convert.ToDouble(heigth) * 2.54).ToString("0.00");
                        info.Num = Convert.ToInt32(textEditNum.Text == "" ? "0" : textEditNum.Text);
                        info.UnitPrice = Convert.ToDouble(textEditunitprice.Text == "" ? "0" : textEditunitprice.Text);
                        changeValue = info.Num * info.UnitPrice;
                        _bll.AddDetailDetail(info, Currbilldetail.BillDetailId, userName);
                        BangDinDetailDetail();

                        MessageBox.Show("添加明细成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                else if (islEdit)
                {
                    if (isClickProduct)//修改产品部分
                    {
                        Bill_Detail_Info info = new Bill_Detail_Info();
                        info.CtmName = textEditCtmName.Text.Trim();
                        info.OrderInfoCode = textEditOrderProductCode.Text.Trim();
                        info.OrderProductName = textEditProductName.Text.Trim();
                        info.Remark = textEditRemark.Text.Trim();
                        info.ListDetail_Detail = null;
                        info.BillDetailId = Currbilldetail.BillDetailId;
                        _bll.UpdataDetail(info);
                        MessageBox.Show("修改产品成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (isClickDetail)//修改明细部分
                    {
                        Bill_Detail_Detail_Info info = CurrInfo;
                        double oldAreas = info.Area;
                        int oldNum = info.Num;
                        info.Area = Convert.ToDouble(textEditAreas.Text == "" ? "0" : textEditAreas.Text);
                        double oldCount = info.Count;

                        //if (oldAreas == 0 && info.Area==0)
                        //{
                        //    if (!oldNum.Equals(textEditNum.Text.Trim()))
                        //    {
                        //        if (Type == 1)
                        //        {
                        //            info.Type = 1;

                        //            info.Count = oldAreas / oldNum * Convert.ToDouble(textEditNum.Text.Trim() == "" ? "0" : textEditNum.Text.Trim()) * Convert.ToDouble(textEditunitprice.Text.Trim() == "" ? "0" : textEditunitprice.Text.Trim());
                        //            info.Area = oldAreas / oldNum * Convert.ToDouble(textEditNum.Text.Trim() == "" ? "0" : textEditNum.Text.Trim());
                        //        }
                        //        else if (Type == 0)
                        //        {
                        //            info.Type = 0;
                        //            info.Count = Convert.ToDouble(textEditNum.Text.Trim() == "" ? "0" : textEditNum.Text.Trim()) * Convert.ToDouble(textEditunitprice.Text.Trim() == "" ? "0" : textEditunitprice.Text.Trim());

                        //        }
                        //    }
                        //}
                        string oldAreasStr;
                        string infoAreaStr;
                        if (oldAreas.ToString().Length <= 4)
                        {
                            oldAreasStr = oldAreas.ToString().Substring(0, oldAreas.ToString().Length);
                        }
                        else
                        {
                            oldAreasStr = oldAreas.ToString();
                        }
                        if (info.ToString().Length <= 4)
                        {
                            infoAreaStr = info.Area.ToString().Substring(0, info.Area.ToString().Length);
                        }
                        else
                        {
                            infoAreaStr = info.Area.ToString();
                        }
                        if ((oldAreas != 0 && info.Area == 0) || (oldAreas == 0 && info.Area != 0) || ((oldAreas != 0 && info.Area != 0) && (!oldAreasStr.Equals(infoAreaStr))))
                        {
                            //double newSingleArea = info.Area / Convert.ToDouble(textEditNum.Text.Trim() == "" ? "0" : textEditNum.Text.Trim());
                            // info.Count = Convert.ToDouble(textEditNum.Text == "" ? "0" : textEditNum.Text) * Convert.ToDouble(textEditunitprice.Text == "" ? "0" : textEditunitprice.Text);
                            if (Type == 1)
                            {
                                info.Type = 1;

                                info.Count = Convert.ToDouble(textEditAreas.Text.Trim() == "" ? "0" : textEditAreas.Text.Trim()) * Convert.ToDouble(textEditunitprice.Text.Trim() == "" ? "0" : textEditunitprice.Text.Trim());
                            }
                            else if (Type == 0)
                            {
                                info.Type = 0;
                                info.Count = Convert.ToDouble(textEditNum.Text.Trim() == "" ? "0" : textEditNum.Text.Trim()) * Convert.ToDouble(textEditunitprice.Text.Trim() == "" ? "0" : textEditunitprice.Text.Trim());

                            }
                        }
                        else//尺寸没改动
                        {
                            if (!oldNum.Equals(textEditNum.Text.Trim()))
                            {
                                if (Type == 1)
                                {
                                    info.Type = 1;
                                    ////double num1 = oldNum * Convert.ToDouble(textEditNum.Text.Trim() == "" ? "0": textEditNum.Text.Trim()) * Convert.ToDouble(textEditunitprice.Text.Trim() == "" ? "0": textEditunitprice.Text.Trim());
                                    ////double num2 = oldNum * Convert.ToDouble(textEditNum.Text.Trim() == "" ? "0" : textEditNum.Text.Trim());
                                    //info.Count = oldAreas / (num1 == 0 ? 1 : num1);
                                    //info.Area = oldAreas / (num2 == 0 ? 1 : num2);
                                    info.Count = oldAreas * Convert.ToDouble(textEditunitprice.Text.Trim() == "" ? "0" : textEditunitprice.Text.Trim());
                                    info.Area = oldAreas;
                                }
                                else if (Type == 0)
                                {
                                    info.Type = 0;
                                    info.Count = Convert.ToDouble(textEditNum.Text.Trim() == "" ? "0" : textEditNum.Text.Trim()) * Convert.ToDouble(textEditunitprice.Text.Trim() == "" ? "0" : textEditunitprice.Text.Trim());

                                }
                            }
                        }

                        info.DetailName = textEditMingxi.Text;
                        info.Spec = textEdit_sqc.Text;
                        //Double weigth = Convert.ToDouble(textEditWeigth.Text);
                        //Double heigth = Convert.ToDouble(textEditHeigth.Text);
                        //info.Spec = weigth.ToString("0.00") + "×" + heigth.ToString("0.00") + "(" + (Convert.ToDouble(weigth) * 2.54).ToString("0.00") + "×" + (Convert.ToDouble(heigth) * 2.54).ToString("0.00");
                        info.Num = Convert.ToInt32(textEditNum.Text == "" ? "0" : textEditNum.Text);
                        //info.Num = Convert.ToInt32(textEditNum.Text == "" ? "0" : textEditNum.Text);
                        info.UnitPrice = Convert.ToDouble(textEditunitprice.Text == "" ? "0" : textEditunitprice.Text);
                        if (checkBoxIsShumashuchu.Checked)
                        {
                            //changeValue = info.Num * info.UnitPrice;  
                        }
                        else
                        {
                            info.Count = info.Num * info.UnitPrice;
                        }


                        _bll.UpdateDetailDetail(info, CurrbilldetaildetailId, userName);

                        BangDinDetailDetail();

                        MessageBox.Show("修改明细成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    islEdit = false;
                    simpleButtonEdit.Enabled = true;
                    simpleButtonSave.Enabled = false;
                    simpleButtonDel.Enabled = true;
                    simpleButtonNew.Enabled = true;
                    splitContainerNew.Panel1Collapsed = true;
                }
                IsCreateBill = false;
            }

        }

        private void simpleButtonEdit_Click(object sender, EventArgs e)
        {
            if (!CheckBillStatus(CurrBill))
            {
                MessageBox.Show("该账单不能编辑，请确认账单状态", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            islEdit = true;
            isNew = false;
            //isDel = false;
            if (islEdit && isClickProduct)
            {
                simpleButtonNew.Enabled = false;
                simpleButtonSave.Enabled = true;
                simpleButtonDel.Enabled = false;
                simpleButtonEdit.Enabled = false;

                splitContainerNew.Panel1Collapsed = false;
                splitContainerDetailOrDDerail.Panel1Collapsed = true;
                splitContainerDetailOrDDerail.Panel2Collapsed = false;
                //if (Currbilldetail != null)
                //{
                //    textEditOrderProductCode.Text = Currbilldetail.OrderInfoCode;
                //    textEditProductName.Text = Currbilldetail.OrderProductName;
                //    textEditCtmName.Text = Currbilldetail.CtmName;
                //    textEditRemark.Text = Currbilldetail.Remark;
                //}
                //else
                //{
                //    MessageBox.Show("请选择一个产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //}

            }
            else if (islEdit && isClickDetail)
            {
                simpleButtonNew.Enabled = false;
                simpleButtonSave.Enabled = true;
                simpleButtonDel.Enabled = false;
                simpleButtonEdit.Enabled = false;

                splitContainerNew.Panel1Collapsed = false;
                splitContainerDetailOrDDerail.Panel1Collapsed = false;
                splitContainerDetailOrDDerail.Panel2Collapsed = true;
            }




        }

        private void simpleButtonDel_Click(object sender, EventArgs e)
        {
            if (!CheckBillStatus(CurrBill))
            {
                MessageBox.Show("该账单不能编辑,确认账单的状态", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (isClickDetail)
            {
                splitContainerNew.Panel1Collapsed = true;
                if (MessageBox.Show("确定要删除 " + CurrInfo.DetailName + "  收费项目吗？如果该明细所在产品只有一个项目会同产品一起删除", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    _bll.DelDetailDetail(CurrInfo.Bill_Detail_DetailId, userName);
                    BangDinDetailDetail();
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (isClickProduct)
            {
                if (!string.IsNullOrEmpty(Currbilldetail.OrderProductName))
                {
                    if (
                        MessageBox.Show("确定要删除产品 " + Currbilldetail.OrderProductName + " ？如果改产品所在账单只有一个产品该账单会一起删除", "确认",
                                        MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        _bll.DeleteProduct(Currbilldetail.BillDetailId, userName);
                        //BangDinDetailDetail();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("请选择一个产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            else if (isClickBill)
            {
                if (MessageBox.Show("确定要删除 " + CurrBill.BillOrderCode + "  账单？", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    _bll.DeleteBill(CurrBill.BillId, userName);
                    // BangDinDetailDetail();
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }

        }
        private void BangDinDetailDetail()
        {

            lists = _Presenter.GetDetailDetail(Currbilldetail.BillDetailId);
            gridControlDetaildetail.DataSource = lists;
        }
        private void splitContainerBottom_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void gridViewBill_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle % 2 == 0)
            {
                e.Appearance.BackColor = Color.LightGray;
            }

        }

        private void simpleButtonCancel_Click(object sender, EventArgs e)
        {
            //  isDel = false;
            isNew = false;
            islEdit = false;
            simpleButtonEdit.Enabled = true;
            simpleButtonSave.Enabled = false;
            simpleButtonDel.Enabled = true;
            simpleButtonNew.Enabled = true;
            splitContainerNew.Panel1Collapsed = true;

        }

        private void ViewProduct_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle % 2 == 0)
            {
                e.Appearance.BackColor = Color.LightGray;
            }
        }

        private void FrmBillEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }

        //开退单
        private void tsBackOrder_Click(object sender, EventArgs e)
        {
            BillView bill = gridViewBill.GetRow(gridViewBill.FocusedRowHandle) as BillView;
            if (bill != null)
            {
                ShopBill.FrmBackOrder fbo = new MINERP.Statistics.ShopBill.FrmBackOrder();
                fbo.ShopId = bill.ShopId;
                fbo.BillId = bill.BillId;
                fbo.ShowDialog();
            }
        }

        private void simpleButtonOpen_Click(object sender, EventArgs e)
        {
            if (simpleButtonOpen.Text.Equals("展开"))
            {
                simpleButtonOpen.Text = "折叠";
                int rowCount = gridViewBill.RowCount;
                for (int i = 0; i < rowCount; i++)
                {
                    gridViewBill.ExpandMasterRow(i);
                }
            }
            else
            {
                simpleButtonOpen.Text = "展开";
                int rowCount = gridViewBill.RowCount;
                for (int i = 0; i < rowCount; i++)
                {
                    gridViewBill.CollapseAllDetails();
                }
            }
        }

        private void simpleButtonExport_Click(object sender, EventArgs e)
        {
            gridControlBill.ExportToExcel(this, "客户账单信息");
        }

        private void ViewProduct_RowClick(object sender, RowClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                //int intDetailId = Convert.ToInt32(((BillDetailView)(ViewProduct.GetRow(e.FocusedRowHandle))).BillDetailId);

                GridView view = sender as GridView;
                Currbilldetail = view.GetFocusedRow() as BillDetailView;
                isDetail = true;// 添加产品
                isClickProduct = true;
                isClickBill = false;
                isClickDetail = false;
                textEditOrderProductCode.Text = Currbilldetail.OrderInfoCode;
                textEditProductName.Text = Currbilldetail.OrderProductName;
                textEditCtmName.Text = Currbilldetail.CtmName;
                textEditRemark.Text = Currbilldetail.Remark;

                lists = _Presenter.GetDetailDetail(Currbilldetail.BillDetailId);
                gridControlDetaildetail.DataSource = lists;
                if (lists.Count > 0)
                {
                    simpleButtonEdit.Enabled = true;
                    simpleButtonSave.Enabled = true;
                    simpleButtonDel.Enabled = true;
                }
            }
        }

        private void textEditHeigth_Leave(object sender, EventArgs e)
        {
            if (textEditWeigth.Text != "" && textEditHeigth.Text != "")
            {
                if (textEditWeigth.Text == "0" || textEditHeigth.Text == "0")
                {
                    return;
                }
                Double weigth = Convert.ToDouble(textEditWeigth.Text);
                Double heigth = Convert.ToDouble(textEditHeigth.Text);
                int num = Convert.ToInt32(textEditNum.Text);
                textEditAreas.Text = (weigth * heigth * 2.54 * 2.54 / 10000 * num).ToString();
                this.textEdit_sqc.Text = weigth.ToString("0.00") + "×" + heigth.ToString("0.00") + "in(" + (weigth * 2.54).ToString("0.00") + "×" + (heigth * 2.54).ToString("0.00") + ")";
            }
            else
            {
                MessageBox.Show("高宽不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        bool isDetailNew = false;
        bool islDetailEdit = false;
        bool isDetailDel = false;
        Bill_Detail_Info CurrDetail = new Bill_Detail_Info();
        SHOP_Bill_Info CurrShopBill = new SHOP_Bill_Info();
        private void simpleButtonDetailAdd_Click(object sender, EventArgs e)
        {
            isDetailNew = true;
            islDetailEdit = false;
            isDetailDel = false;
            textBoxDeatialProductName.Text = "";
            textBoxDetailBarCode.Text = "";

        }
        BillView CurrBill = new BillView();
        private void gridViewBill_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            int intRow = e.FocusedRowHandle;
            if (intRow >= 0)
            {



                BillView view = (BillView)gridViewBill.GetRow(intRow);
                this.CurrBill = view;
                //textEditOrderProductCode.Text=view.
            }
        }

        private void gridViewBill_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                this.CurrBill = (BillView)gridViewBill.GetRow(e.RowHandle) as BillView;
                isClickProduct = false;
                isClickBill = true;
                isClickDetail = false;

            }

        }

        private void gridControlBill_Click(object sender, EventArgs e)
        {

        }

        private void checkBoxIsShumashuchu_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxIsShumashuchu.Checked)
            {
                textEditMingxi.Enabled = false;
                //textEditMingxi.Text = "数码输出";
                Type = 1;
            }
            else
            {
                Type = 0;
                textEditMingxi.Enabled = true;
            }
        }

        private void textEditAreas_Enter(object sender, EventArgs e)
        {
            if (textEditWeigth.Text != "" && textEditHeigth.Text != "")
            {
                if (textEditWeigth.Text == "0" || textEditHeigth.Text == "0")
                {
                    return;
                }
                Double weigth = Convert.ToDouble(textEditWeigth.Text);
                Double heigth = Convert.ToDouble(textEditHeigth.Text);
                int num = Convert.ToInt32(textEditNum.Text);
                textEditAreas.Text = (weigth * heigth * 2.54 * 2.54 / 10000 * num).ToString("0.0000");
                this.textEdit_sqc.Text = weigth.ToString("0.00") + "×" + heigth.ToString("0.00") + "in(" + (weigth * 2.54).ToString("0.00") + "×" + (heigth * 2.54).ToString("0.00") + ")";
            }
            else
            {
                MessageBox.Show("高宽不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        bool isCheckProduct = false;
        private void simpleButtonCheckProduct_Click(object sender, EventArgs e)
        {
            string strProduct = textEditOrderProductCode.Text.Trim();
            if (strProduct.Length != 10 || !strProduct.StartsWith("2"))
            {
                MessageBox.Show("输入产品编号不正确，请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                OrderInfo_Info info = _Presenter.GetProduct(strProduct);
                if (info != null)
                {
                    isCheckProduct = true;
                    this.textBoxDeatialProductName.Text = info.OrderProductName;
                    this.textEditCtmName.Text = info.CtmName;
                    this.textEditRemark.Text = info.Remark;
                }
                else
                {
                    MessageBox.Show("未找到该产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

            }
        }

        private void simpleButton_Click(object sender, EventArgs e)
        {
            if (CurrBill.status == (int)ShopBillStatus.CheckedOut)
            {
                MessageBox.Show("该产品已经结账不能转移", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            FrmDivert frm = null;
            if (isClickBill)
            {
                frm = new FrmDivert(CurrBill.BillId, 0);

            }
            else if (isClickProduct)
            {
                frm = new FrmDivert(CurrBill.BillId, Currbilldetail.BillDetailId);
            }
            else
            {
                MessageBox.Show("请点击要移动的订单或者产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frm.ShowDialog();

        }

        private void gridViewBill_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                BillView view = (BillView)gridViewBill.GetRow(e.RowHandle) as BillView;
                if (e.Column.Name == "Status")
                {
                    switch (view.status)
                    {
                        case (int)ShopBillStatus.CheckedOut:
                            e.DisplayText = "已结账";
                            break;
                        case (int)ShopBillStatus.UnCheckOut:
                            e.DisplayText = "未结账";
                            break;
                        case (int)ShopBillStatus.Delete:
                            e.DisplayText = "已删除";
                            break;
                        case (int)ShopBillStatus.Diverted:
                            e.DisplayText = "已转移";
                            break;
                        default:

                            e.DisplayText = "已转移";
                            break;


                    }
                }
            }
        }

        private void simpleButtonReturn_Click(object sender, EventArgs e)
        {
            if (isClickBill)
            {
                if (MessageBox.Show("确定要将账单ID为" + CurrBill.BillId + "的账单退单吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    //返整张账单CurrBill.BillId
                    int rtnInt = _bll.CreateOutReturn(CurrBill.BillId, userName);
                    if (rtnInt > 0)
                    {
                        MessageBox.Show("整张账单创建成功，请在该商户下查找", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else
                {
                    return;
                }

            }
            else if (isClickProduct)
            {
                //返单个产品CurrBill.BillId, Currbilldetail.BillDetailId
                if (MessageBox.Show("确定要将明细号为" + Currbilldetail.BillDetailId + "的产品退单吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    int rtnInt = _bll.CreateOutReturn2(Currbilldetail.BillDetailId, userName);
                    if (rtnInt > 0)
                    {
                        MessageBox.Show("退单产品账单创建成功，请在该商户下查找", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else
                {
                    return;
                }

            }
            else
            {
                MessageBox.Show("请点击要开返单的账单或者产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }
        bool IsCreateBill = false;
        private void simpleButtonPartReturn_Click(object sender, EventArgs e)
        {
            IsCreateBill = true;
            Shop_Info shop=(Shop_Info)(cmbShopList.SelectedItem);
            if (shop != null)
            {
                int intVal = _bll.CreateShopBillTitle(shop.ShopId, base.userName);
                if (intVal != -1)
                {
                    textEditbianhao.Text = intVal.ToString();
                    simpleButtonOK_Click(sender, e);
                    MessageBox.Show("账单号为：" + intVal + "的账单头创建成功，请为其添加相应内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else {
                    MessageBox.Show("创建账单头失败，请稍后重新尝试创建", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }










    }
}
